﻿using System.Configuration;
using System.Web;

namespace Buy4Now.Shop4NowGM.Application.Shared.BusinessLogic
{
    public enum ImageDirectory
    {
        SmallImageFolderName,
        MediumImageFolderName,
        LargeImageFolderName,
        ExtraLargeImageFolderName,
        HighlightImageFolderName,
        HighlightSecImageFolderName,
        BrandImageFolderName,
        SwatchImageFolderName,
        HomepageImageFolderName,
        XSmallImageFolderName,
        CategoryImageFolderName,
        BuyingGuideImageFolderName,
        ZoomImageFolderName
    }
    public enum ImageType
    {
        STANDARDIMAGE = 1,
        PRODUCTIMAGE = 2
    }
    public static class ImageUrlHelper
    {
        #region Public Methods
        public static string ImageUrl()
        {
            string siteImagesUrl = string.Empty;
            if (ConfigurationManager.AppSettings["SiteImages"] != null)
            {
                siteImagesUrl = ConfigurationManager.AppSettings["SiteImages"].ToString();

                if (HttpContext.Current.Request.ServerVariables["HTTPS"].ToString() != "off")
                {
                    if (ConfigurationManager.AppSettings["SiteImagesSecure"] != null)
                    {
                        siteImagesUrl = ConfigurationManager.AppSettings["SiteImagesSecure"];
                    }
                    else
                    {
                        siteImagesUrl = siteImagesUrl.Replace("http", "https");
                    }
                }

                if (siteImagesUrl.EndsWith("/").Equals(false))
                {
                    siteImagesUrl = siteImagesUrl + "/";
                }
            }
            if (string.IsNullOrEmpty(siteImagesUrl))
            {
                siteImagesUrl = "images/";
            }

            return siteImagesUrl;
        }
        #endregion
    }

    public static class ImageUtility
    {
        #region Public Properties
        public static string ImageFolderUri
        {
            get
            {
                return Settings.Instance.Get<string>("ImageFolderUri");
            }
        }
        public static string ImageFolderLocation
        {
            get
            {
                return Settings.Instance.Get<string>("ImageFolderLocation");
            }
        }
        public static string ProductImageFolderUri
        {
            get
            {
                return Settings.Instance.Get<string>("ProductImageFolderUri");
            }
        }

        public static string SmallImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("SmallImageFolderName");
            }
        }
        public static string XSmallImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("XSmallImageFolderName");
            }
        }
        public static string MediumImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("MediumImageFolderName");
            }
        }

        public static string LargeImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("LargeImageFolderName");
            }
        }

        public static string ZoomImageFolderName
        {
            get
            {
                return Settings.Instance.GetSafe<string>("ZoomImageFolderName");
            }
        }

        public static string ExtraLargeImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("ExtraLargeImageFolderName");
            }
        }

        public static string HighlightImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("HighlightImageFolderName");
            }
        }

        public static string HighlightSecImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("HighlightSecImageFolderName");
            }
        }

        public static string BrandImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("BrandImageFolderName");
            }
        }

        public static string SwatchImageFolderName
        {
            get
            {
                return Settings.Instance.Get<string>("SwatchImageFolderName");
            }
        }

        public static string HomepageImageFolderName
        {
            get
            {
                return Settings.Instance.GetSafe<string>("HomepageFolderName");
            }
        }

        public static string CategoryImageFolderName
        {
            get
            {
                return Settings.Instance.GetSafe<string>("CategoryImageFolderName");
            }
        }

        public static string BuyingGuideImageFolderName
        {
            get
            {
                return Settings.Instance.GetSafe<string>("BuyingGuideImageFolderName");
            }
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// Builds the image URL.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="imageName">Name of the image.</param>
        /// <returns></returns>
        public static string BuildImageUrl(ImageDirectory directory, string imageName)
        {
            ImageType imageType = ImageType.STANDARDIMAGE;
            if (imageName == null)
            {
                return string.Empty;
            }

            string folderName = string.Empty;

            switch (directory)
            {
                case ImageDirectory.SmallImageFolderName:
                    folderName = SmallImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.XSmallImageFolderName:
                    folderName = XSmallImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.MediumImageFolderName:
                    folderName = MediumImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.LargeImageFolderName:
                    folderName = LargeImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.ExtraLargeImageFolderName:
                    folderName = ExtraLargeImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.HighlightImageFolderName:
                    folderName = HighlightImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.HighlightSecImageFolderName:
                    folderName = HighlightSecImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.BrandImageFolderName:
                    folderName = BrandImageFolderName;
                    break;
                case ImageDirectory.SwatchImageFolderName:
                    folderName = SwatchImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.HomepageImageFolderName:
                    folderName = HomepageImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                case ImageDirectory.CategoryImageFolderName:
                    folderName = CategoryImageFolderName;
                    imageType = ImageType.STANDARDIMAGE;
                    break;
                case ImageDirectory.BuyingGuideImageFolderName:
                    folderName = BuyingGuideImageFolderName;
                    imageType = ImageType.STANDARDIMAGE;
                    break;

                case ImageDirectory.ZoomImageFolderName:
                    folderName = ZoomImageFolderName;
                    imageType = ImageType.PRODUCTIMAGE;
                    break;
                default:
                    folderName = "";
                    break;
            }

            if (folderName.Length > 0)
            {
                if (imageType == ImageType.PRODUCTIMAGE)
                {
                    return ProductImageFolderUri + "/" + folderName + "/" + imageName;
                }
                else
                {
                    return ImageFolderUri + "/" + folderName + "/" + imageName;
                }
            }

            return "";
        }

        #endregion

    }
}
